//
//  UserData.m
//  yuikemall
//
//  Created by Gang.Wang on 12-5-21.
//  Copyright (c) 2012年 yuike. All rights reserved.
//

#import "UserDataforLikeit.h"
#import "productUseUpTable.h"

#import "EntityManager.h"



@interface UserDataforLikeit ()
{
    NSString * userDataPath;
}

@property (nonatomic, strong) NSString * userDataPath;
@end

@implementation UserDataforLikeit

@synthesize userDataPath;

SYNTHESIZE_SINGLETON_FOR_CLASS(UserDataforLikeit)

- (void) dealloc
{
    YRelease(userDataPath)

    [super dealloc];
}


- (id) init
{
    // 保持 @"userdata.sqlite" 命名，和前一个版本 兼容。。。
    self.userDataPath = [YPath Combine:kDocuments combinePath:@"userdata_v2.sqlite"];
    
    self = [super initWithDataBasePath:self.userDataPath];
    
    if (self) 
    {
    
    }
    return self;
}



- (NSArray *) allUserUpRecords
{
    return [self allRecords:CLASSNAME(productUseUpTable)];
}



- (NSArray *) upRecordsWithArgument:(NSDictionary *) arg
{
    if (!arg) 
    {
        return nil;
    }
    
    NSArray * rows = [self.entityManager find: CLASSNAME(productUseUpTable) withArgument:arg];    
    return rows;
}


-(void)clearRecordBefore:(NSDate*)datex {
    
    if (!datex) return;
    
    NSDictionary * dict = [NSDictionary dictionaryWithObjectsAndKeys:
                           [NSString stringWithFormat:@"<%llf", [datex timeIntervalSince1970]],
                           @"modifyTime", nil];
    [self.entityManager delete:CLASSNAME(productUseUpTable) withArgument:dict];
}











- (productUseUpTable *) upRecordWithArgument:(NSDictionary *) arg
{
    if (!arg) 
    {
        return nil;
    }
    
    NSArray * rows = [self upRecordsWithArgument:arg];
    if (!rows ||
        [rows count] <= 0) 
    {
        return nil;
    }
    
    return (productUseUpTable *)  [rows objectAtIndex:0];
    
}

- (productUseUpTable *) upRecordWithUrl:(NSString *) url
{
    NSDictionary * dic = [NSDictionary dictionaryWithObjectsAndKeys:
                          [NSString stringWithFormat:@"='%@'", url],
                          @"upUrlString", nil];
    
    return [self upRecordWithArgument:dic];
}


//- (productUseUpTable *) upRecordWithShortUrl:(NSString *) shortUrl
//{
//    NSDictionary * dic = [NSDictionary dictionaryWithObjectsAndKeys:
//                           [NSString stringWithFormat:@"='%@'", shortUrl],
//                          @"upShortUrlString", nil];
//    
//    return  [self upRecordWithArgument:dic];
//}

#pragma mark - Public Method
- (void) addUpUrlRecord:(productUseUpTable *) record
{
    if (!record) 
    {
        return;
    }
        
    productUseUpTable * obj = (productUseUpTable *)[self.entityManager create:CLASSNAME(productUseUpTable)];
    obj.upUrlString = record.upUrlString ;
    obj.upShortUrlString = record.upShortUrlString ;
    obj.upCount = record.upCount;
    obj.userUped = record.userUped;
    obj.rowId = record.rowId;    
    obj.modifyTime = record.modifyTime;
    [obj save];
}

//- (BOOL) isUpedWithShortUrl:(NSString *) shortUrl
//{
//    productUseUpTable * obj = [self upRecordWithShortUrl:shortUrl];
//    if (!obj) 
//    {
//        return FALSE;
//    }
//    
//    return obj.userUped == YES ?  TRUE : FALSE;
//}

- (BOOL) isUpedWithUrl:(NSString *) url
{
    productUseUpTable * obj = [self upRecordWithUrl:url];
    
    if (!obj) 
    {
        return FALSE;
    }
    return obj.userUped == YES ?  TRUE : FALSE;
}






- (void) upUrl:(NSString *) url withShortUrl:(NSString *) shortUrl
{
    [self upUrl:url withShortUrl:shortUrl upCount:-1 ];
}

- (void) upUrl:(NSString *) url withShortUrl:(NSString *) shortUrl upCount:(NSInteger)count
{
    [self upUrl:url withShortUrl:shortUrl userUped:TRUE upCount:count forceUpdate:TRUE];
}

- (productUseUpTable *) getRecordWithUrl:(NSString *) url {
    return [self upRecordWithUrl:url];
}
//- (productUseUpTable *) getRecordWithShortUrl:(NSString *) shortUrl {
//    return [self upRecordWithShortUrl:shortUrl];
//}

- (void) upUrl:(NSString *) url withShortUrl:(NSString *) shortUrl userUped:(BOOL) userUped upCount:(NSInteger) count forceUpdate:(BOOL) forceUpdated
{
    if ([NSString isNilOrEmpty:url] &&
        [NSString isNilOrEmpty:shortUrl]) 
    {
        return;
    }
    
    
    productUseUpTable * upedObj = [self upRecordWithUrl:url];
//    if (!upedObj) 
//    {
//        upedObj = [self upRecordWithUrl:url];
//    }
    
    if (upedObj && !forceUpdated) 
    {
        return;
    }
    
    productUseUpTable * obj = (productUseUpTable *)[self.entityManager create:CLASSNAME(productUseUpTable)];
    obj.upUrlString = [NSString isNilOrEmpty:url] ? upedObj.upUrlString : url ;
    obj.upShortUrlString = [NSString isNilOrEmpty:shortUrl] ? upedObj.upShortUrlString  : shortUrl ;
    
    obj.upCount = count < 0 ? upedObj.upCount : count;
    obj.userUped = userUped;  
    obj.modifyTime = [[NSDate date] timeIntervalSince1970];
    
    if (upedObj &&
        upedObj.rowId > 0) 
    {
        obj.rowId = upedObj.rowId;
    }
    [obj save];

}



- (void) upProductUpCountWithLongUrl:(NSString *) longUrl upCount:(NSInteger ) count
{
    if ([NSString isNilOrEmpty:longUrl]) 
    {
        return;
    }
    
    [self upUrl:longUrl withShortUrl:nil userUped:[[UserDataforLikeit sharedInstance] isUpedWithUrl:longUrl] upCount:count forceUpdate:TRUE];
}
//- (void) upProductUpCountWithShorUrl:(NSString *) shortUrl upCount:(NSInteger ) count
//{
//    if ([NSString isNilOrEmpty:shortUrl]) 
//    {
//        return;
//    }
//    
//    
//    [self upUrl:nil withShortUrl:shortUrl userUped:[[UserDataforLikeit sharedInstance] isUpedWithShortUrl:shortUrl] upCount:count forceUpdate:TRUE];
//}

- (NSInteger) upCountWithUrl:(NSString *) url
{
    NSDictionary * dic = [NSDictionary dictionaryWithObjectsAndKeys:
                           [NSString stringWithFormat:@"='%@'", url],
                          @"upUrlString", nil];

    productUseUpTable * obj = [self upRecordWithArgument:dic];
    if (obj) 
    {
        return obj.upCount;
    }
    return 0;
}
//- (NSInteger) upCountWithShortUrl : (NSString *) shortUrl
//{
//    NSDictionary * dic = [NSDictionary dictionaryWithObjectsAndKeys:
//                           [NSString stringWithFormat:@"='%@'", shortUrl],
//                          @"upShortUrlString", nil];
//    
//    productUseUpTable * obj = [self upRecordWithArgument:dic];
//    if (obj) 
//    {
//        return obj.upCount;
//    }
//    return 0;
//}


- (void) addShortUrl:(NSString *) shortUrl withSourceUrl:(NSString *) url
{
    if ([NSString isNilOrEmpty:shortUrl] ||
        [NSString isNilOrEmpty:url]) 
    {
        return;
    }
    
    [self addShortUrl:shortUrl withSourceUrl:url forceUpdate:TRUE];
}

- (void) addShortUrl:(NSString *) shortUrl withSourceUrl:(NSString *) url forceUpdate:(BOOL) forceUpdated
{
    BOOL bUped = [self isUpedWithUrl:url];
    [self upUrl:url withShortUrl:shortUrl userUped:bUped upCount:-1 forceUpdate:forceUpdated];    
}


- (NSString *) shortUrlWithSourceUrl:(NSString *) sourceUrl
{
    if ([NSString isNilOrEmpty:sourceUrl]) {
        return nil;
    }
    
    productUseUpTable * obj = [self upRecordWithUrl:sourceUrl];
    if(!obj)
    {
        return nil;
    }
    
    return obj.upShortUrlString;

}
//- (NSString *) sourceUrlWithShortUrl : (NSString *) shortUrl
//{
//    if ([NSString isNilOrEmpty:shortUrl]) {
//        return nil;
//    }
//    
//    productUseUpTable * obj = [self upRecordWithShortUrl:shortUrl];
//    if(!obj)
//    {
//        return nil;
//    }
//    
//    return obj.upUrlString;
//}


-(BOOL) isLikeit:(NSString*)url_long { // 和上个版本的 url_long 兼容
    productUseUpTable * obj = [self upRecordWithUrl:url_long];    
    if (!obj) {
        return FALSE;
    }
    return obj.userUped == YES ?  TRUE : FALSE;
}
-(void) likeit:(NSString*)url_long {
    [self upUrl:url_long withShortUrl:@"url_short" userUped:TRUE upCount:-1 forceUpdate:TRUE];    
}


@end
